package view;

import Bean.Room;
import control.RoomManagerControl;
import java.awt.HeadlessException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;

public class RoomTypeDialog extends RootDialog
{

    RoomManagerControl rmControl = new RoomManagerControl();
    Room Room = new Room();
    String choice;
    int index;
    RoomCategoryPanel Panel;

    public RoomTypeDialog(RoomCategoryPanel Panel)
    {
        initComponents();
        this.Panel = Panel;
        this.setTitle("Thiếp lập phòng");
        this.setResizable(false);
        jCBThemloaiphong.setSelected(true);
        jCBSualoaiphong.setSelected(false);

        jTDanhsachkieuphong.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent me)
            {
                index = jTDanhsachkieuphong.getSelectedRow();
                choice = jTDanhsachkieuphong.getValueAt(index, 0).toString();
                if (jCBSualoaiphong.getSelectedObjects() != null)
                {
                    Room = rmControl.getRoomType(choice);
                    jTFLoaiphong.setText(choice);
                    jTFLoaiphong.setEnabled(false);
                    jTFGiaphong.setText(Integer.toString(Room.getCostroom()));
                }
            }
        });
        Init();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        buttonGroup4 = new javax.swing.ButtonGroup();
        buttonGroup5 = new javax.swing.ButtonGroup();
        buttonGroup6 = new javax.swing.ButtonGroup();
        buttonGroup7 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jTFLoaiphong = new javax.swing.JTextField();
        jTFGiaphong = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jBThem = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTDanhsachkieuphong = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jCBSualoaiphong = new javax.swing.JCheckBox();
        jCBThemloaiphong = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 153));
        jLabel1.setText("Loại phòng");

        jTFLoaiphong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jTFGiaphong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 51, 153));
        jLabel2.setText("Giá phòng");

        jBThem.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jBThem.setText("Thêm");
        jBThem.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jBThemActionPerformed(evt);
            }
        });

        jTDanhsachkieuphong.setModel(rmControl.getRoomType());
        jScrollPane2.setViewportView(jTDanhsachkieuphong);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 51, 153));
        jLabel3.setText("Danh sách cập nhật");

        jCBSualoaiphong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jCBSualoaiphong.setForeground(new java.awt.Color(0, 51, 153));
        jCBSualoaiphong.setText("Sửa loại phòng");
        jCBSualoaiphong.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jCBSualoaiphongActionPerformed(evt);
            }
        });

        jCBThemloaiphong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jCBThemloaiphong.setForeground(new java.awt.Color(0, 51, 153));
        jCBThemloaiphong.setText("Thêm loại phòng");
        jCBThemloaiphong.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jCBThemloaiphongActionPerformed(evt);
            }
        });

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/room_type.png"))); // NOI18N

        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/divider.png"))); // NOI18N

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(0, 51, 153));
        jLabel6.setText("VNĐ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(40, 40, 40)
                                        .addComponent(jLabel5))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(9, 9, 9)
                                        .addComponent(jCBSualoaiphong)
                                        .addGap(49, 49, 49)
                                        .addComponent(jCBThemloaiphong)))
                                .addContainerGap())
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jTFGiaphong))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                                        .addComponent(jTFLoaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jBThem, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(29, 29, 29))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 29, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(99, 99, 99)
                        .addComponent(jLabel3)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel4))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCBSualoaiphong)
                    .addComponent(jCBThemloaiphong))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTFLoaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(31, 31, 31))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jBThem, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTFGiaphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(24, 24, 24)
                .addComponent(jLabel5)
                .addGap(17, 17, 17)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBThemActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jBThemActionPerformed
    {//GEN-HEADEREND:event_jBThemActionPerformed

        if (jBThem.getText().equals("Thêm"))
        {
            if (rmControl.roomTypeIsExits(jTFLoaiphong.getText()))
            {
                JOptionPane.showMessageDialog(null, "Lỗi khi nhập");
            }
            else
            {
                try
                {

                    Room.setTyperoom(jTFLoaiphong.getText());
                    Room.setCostroom(Integer.parseInt(jTFGiaphong.getText()));
                    if (isNotNullNotEmptyNotWhiteSpace(jTFLoaiphong.getText()) == false || (Room.getCostroom() <= 0) || Room.getTyperoom().equals("") || jTFGiaphong.getText().equals(""))
                    {
                        JOptionPane.showMessageDialog(null, "Lỗi khi nhập");
                    }
                    else
                    {
                        if (rmControl.addRoomTtype(Room))
                        {
                            JOptionPane.showMessageDialog(null, "Nhập thành công");
                        }
                        else
                        {
                            JOptionPane.showMessageDialog(null, "Lỗi khi nhập");
                        }
                        jTDanhsachkieuphong.setModel(rmControl.getRoomType());
                        choice = null;
                        reset();
                    }
                }
                catch (NumberFormatException | HeadlessException ex)
                {
                    JOptionPane.showMessageDialog(null, "Lỗi khi nhập");
                }
            }
        }
        else
        {
            try
            {
                Room.setTyperoom(jTFLoaiphong.getText());
                Room.setCostroom(Integer.parseInt(jTFGiaphong.getText()));
                if (isNotNullNotEmptyNotWhiteSpace(jTFLoaiphong.getText()) == false || (Room.getCostroom() <= 0) || Room.getTyperoom().equals("") || jTFGiaphong.getText().equals(""))
                {
                    JOptionPane.showMessageDialog(null, "Lỗi khi nhập");
                }
                else
                {
                    if (rmControl.editTypeRoom(Room))
                    {
                        JOptionPane.showMessageDialog(null, "Nhập thành công");
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(null, "Lỗi khi nhập");
                    }
                    jTDanhsachkieuphong.setModel(rmControl.getRoomType());
                    reset();
                    Panel.reset();

                }
            }
            catch (NumberFormatException | HeadlessException ex)
            {
                JOptionPane.showMessageDialog(null, "Lỗi khi nhập");
            }
        }

    }//GEN-LAST:event_jBThemActionPerformed

    private void jCBSualoaiphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBSualoaiphongActionPerformed
        reset();
        if (jCBSualoaiphong.getSelectedObjects() != null)
        {
            jCBThemloaiphong.setSelected(false);
            jBThem.setText("Sửa");
            if (choice != null)
            {
                Room = rmControl.getRoomType(choice);
                jTFLoaiphong.setText(choice);
                jTFLoaiphong.setEnabled(false);
                jTFGiaphong.setText(Integer.toString(Room.getCostroom()));
            }
        }
        else
        {
            jCBThemloaiphong.setSelected(true);
            jTFLoaiphong.setEnabled(true);
            jCBSualoaiphong.setSelected(false);
            jBThem.setText("Thêm");
        }


    }//GEN-LAST:event_jCBSualoaiphongActionPerformed

    private void jCBThemloaiphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBThemloaiphongActionPerformed
        reset();
        if (jCBThemloaiphong.getSelectedObjects() != null)
        {
            jTFLoaiphong.setEnabled(true);
            jCBSualoaiphong.setSelected(false);
            jBThem.setText("Thêm");
        }
        else
        {
            jCBSualoaiphong.setSelected(true);
            jCBThemloaiphong.setSelected(false);
            jBThem.setText("Sửa");
            if (choice != null)
            {
                Room = rmControl.getRoomType(choice);
                jTFLoaiphong.setText(choice);
                jTFLoaiphong.setEnabled(false);
                jTFGiaphong.setText(Integer.toString(Room.getCostroom()));
            }
        }
    }//GEN-LAST:event_jCBThemloaiphongActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.ButtonGroup buttonGroup4;
    private javax.swing.ButtonGroup buttonGroup5;
    private javax.swing.ButtonGroup buttonGroup6;
    private javax.swing.ButtonGroup buttonGroup7;
    private javax.swing.JButton jBThem;
    private javax.swing.JCheckBox jCBSualoaiphong;
    private javax.swing.JCheckBox jCBThemloaiphong;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTDanhsachkieuphong;
    private javax.swing.JTextField jTFGiaphong;
    private javax.swing.JTextField jTFLoaiphong;
    // End of variables declaration//GEN-END:variables

    private void reset()
    {
        jTFLoaiphong.setText(null);
        jTFGiaphong.setText(null);
    }

    public static boolean isNotNullNotEmptyNotWhiteSpace(final String string)
    {
        return string != null && !string.isEmpty() && !string.trim().isEmpty();
    }
}
